package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleMaintainRelateDao;
import com.sugon.entity.ModuleMaintainOptionsEntity;
import com.sugon.entity.ModuleMaintainPlanItemEntity;
import com.sugon.service.ModuleMaintainRelateService;
import com.sugon.utils.Constant;
import com.sugon.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模具维护规程关联Service实现类
 *
 * @author sugon
 * @date 2019-08-09 08:58:40
 */
@Slf4j
@Service("moduleMaintainRelateService")
public class ModuleMaintainRelateServiceImpl implements ModuleMaintainRelateService {

    @Autowired
    private ModuleMaintainRelateDao moduleMaintainRelateDao;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public ModuleMaintainPlanItemEntity queryObject(String id) {
        return moduleMaintainRelateDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleMaintainOptionsEntity> queryListByPlan(Map<String, Object> map) {
        return moduleMaintainRelateDao.queryListByPlan(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotalByPlan(Map<String, Object> map) {
        return moduleMaintainRelateDao.queryTotalByPlan(map);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public int queryIsHave(Map<String, Object> map) {
        return moduleMaintainRelateDao.queryTotalByPlan(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleMaintainPlanItemEntity> queryList(Map<String, Object> map) {
        return moduleMaintainRelateDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleMaintainRelateDao.queryTotal(map);
    }

    /**
     * @param moduleMaintainRelate 实体
     * @return
     */
    @Override
    public int save(ModuleMaintainPlanItemEntity moduleMaintainRelate) {
        moduleMaintainRelate.setId(RandomUtil.randomString(32));
        moduleMaintainRelate.setDelFlag(Constant.IS_NOT_DEL_INT);
        return moduleMaintainRelateDao.save(moduleMaintainRelate);
    }

    /**
     * @param moduleMaintainRelate 实体
     * @return
     */
    @Override
    public int update(ModuleMaintainPlanItemEntity moduleMaintainRelate) {
        return moduleMaintainRelateDao.update(moduleMaintainRelate);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleMaintainRelateDao.delete(id);
    }

    /**
     * @param map
     * @return
     */
    @Override
    public int doDeleteOption(Map<String, Object> map) {
        String planID = map.get("planID") + "";
        String opIDS = map.get("opIDS") + "";
        int total = 0;
        if (StringUtils.isNotEmpty(opIDS) && StringUtils.isNotEmpty(planID)) {
            String[] opIDSArr = opIDS.split(",");
            Map<String, Object> param = new HashMap<>();
            for (String id : opIDSArr) {
                param.put("planID", planID);
                param.put("opID", id);
                int num = moduleMaintainRelateDao.queryIsHave(param);
                if (num > 0) {
                    try {
                        moduleMaintainRelateDao.deleteByOptAndModule(param);
                        total++;
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }
        return total;
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleMaintainRelateDao.deleteBatch(ids);
    }
}
